﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace MvaZimuConvert
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String filename = "";
            if (DialogResult.OK==openFileDialog1.ShowDialog())
            {
                textBox1.Text = openFileDialog1.FileName;
                filename = openFileDialog1.FileName;
            }
            else
            {
                return;
            }



            try
            {
                String str = File.ReadAllText(filename);
                str = str.Substring(str.IndexOf("<div region=\"subtitleArea\">"));
                string[] arr = Regex.Split(str, "<p ttm:role=\"caption\"", RegexOptions.IgnoreCase);
                System.Collections.ArrayList al = new System.Collections.ArrayList();


                //<p ttm:role="caption" xml:id="subtitle1" end="00:00:04:00" begin="00:00:00:20"> [Music] </p>
                for (int i = 1; i < arr.Length; i++)
                {

                    string xuhao = "";
                    string begin = "";
                    string end = "";
                    string zimu = "";
                    //解析数字
                    xuhao = GetMidStr(arr[i], "subtitle", "\" end");
                    //解析开始时间
                    begin = GetMidStr(arr[i], "begin=\"", "\">");
                    //解析结束时间
                    end = GetMidStr(arr[i], "end=\"", "\" begin");
                    //解析字幕
                    zimu = GetMidStr(arr[i], ">", "</p>");
                    al.Add(xuhao);
                    al.Add(begin + "-->" + end);
                    al.Add(zimu);
                }
                string[] arrRes = new string[al.Count];
                for (int i = 0; i < al.Count; i++)
                {
                    arrRes[i] = al[i].ToString();
                }
                string outFileName=DateTime.Now.ToString("yyyyMMddHHmmss")+".srt";
                File.WriteAllLines(outFileName, arrRes);

                MessageBox.Show("完成:" + outFileName);
            }
            catch(Exception ex)
            {
                MessageBox.Show("错误信息："+ex.Message);
            }



        }



        public string GetMidStr(string str, string startStr, string endStr)
        {
            //<p ttm:role="caption" xml:id="subtitle1" end="00:00:04:00" begin="00:00:00:20"> [Music] </p>
            int iBeginIndex = 0;
            iBeginIndex = str.IndexOf(startStr);
            if (iBeginIndex == -1)
            {
                return "";
            }
            int iEndIndex = -1;
            iEndIndex = str.IndexOf(endStr);
            if (iEndIndex == -1)
            {
                return "";
            }
            string result = str.Substring(iBeginIndex + startStr.Length, iEndIndex - iBeginIndex - startStr.Length);
            return result;

        }
    }
}
